<?php
require_once("constants.php");
class MySQLDB
{
   var $connection;
   function __construct(){
      $this->connection = mysql_connect(DB_MAINSERVER, DB_MAINUSER, DB_MAINPASS);
      if ($this->database_error()) {
	 echo 'Es ist ein Verbindungsproblem aufgetreten, bitte probieren Sie es in wenigen Minuten noch einmal!';
	 exit;
      }
      mysql_select_db(DB_MAINNAME, $this->connection);
      if ($this->database_error()) {
         echo 'Es ist ein Datenbankfehler aufgetreten, bitte probieren Sie es in wenigen Minuten noch einmal!';
         exit;
      }
      mysql_query('SET CHARACTER SET utf8', $this->connection);
      mysql_query('SET NAMES utf8', $this->connection);
   } // __construct END

   function query($query, $save = 0){
	 if ($save == 1) { $start = microtime(true); }
	 $result = mysql_query($query, $this->connection)or die("DB Error occurred:".$query."<br />".mysql_error()."[".mysql_errno()."]");
	 if ($save == 1) {
	    $end = microtime(true);
	    $time = $end - $start;
	    $query = mysql_real_escape_string($query); 
	    $insert = 'Insert into tblqueries (id, query, time_in_sec, date) VALUE (DEFAULT,"'.$query.'", '.str_replace(',', '.', $time).', DEFAULT);';
	
	    mysql_query($insert, $this->connection);
	 }
	 return $result;
   }   
   
   function database_num_rows($database_result) {
      return mysql_num_rows($database_result);
   } 
   
   function database_fetch_assoc($database_result) {
      return mysql_fetch_assoc($database_result);
   }
   
   function database_fetch_array($database_result) {
      return mysql_fetch_array($database_result);
   } 
   
   function database_affected_rows($database_result) {
          return mysql_affected_rows($database_result);
   }
   
   function database_insert_id() {
          return mysql_insert_id($this->connection);
   } 
   
   function database_error() {
          return mysql_error($this->connection);
   }
   
   function database_close() {
          mysql_close($this->connection);
   } 
function table_exists ($table) { 
	$tables = mysql_list_tables ($this->connection); 
	while (list ($temp) = mysql_fetch_array ($tables)) {
		if ($temp == $table) {
			return TRUE;
		}
	}
	return FALSE;
}
}